package com.hangwei.paperless.view.repair;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.hangwei.paperless.App;
import com.hangwei.paperless.MainRepairActivity;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.DictBean;
import com.hangwei.paperless.model.entity.CameraEvent;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.NFCCallBack;
import com.hangwei.paperless.view.BaseView;
import com.hangwei.paperless.view.SealHelper;
import com.hangwei.paperless.view.repair.accept.AcceptItem0;
import com.hangwei.paperless.view.repair.accept.AcceptItem1;
import com.hangwei.paperless.view.repair.accept.AcceptItem2;
import com.hangwei.paperless.view.repair.accept.AcceptItem3;
import com.hangwei.paperless.view.repair.accept.AcceptItem4;
import com.hangwei.paperless.view.repair.accept.AcceptItem5;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/10/14.
 */

public class AcceptCheck extends BaseView<MainRepairActivity, AcceptCheckInfo> {

    private AcceptItem0 mItem0;
    private AcceptItem1 mItem1;
    private AcceptItem2 mItem2;
    private AcceptItem3 mItem3;
    private AcceptItem4 mItem4;
    private AcceptItem5 mItem5;
    private EditText mEtRemark;
    public static List<DictBean> mAcceptStatus;
    public ScrollView mScrollView;
    private boolean cameraReload = false;
    private int mScrollY;
    private RelativeLayout mFlUserSeal;
    private RelativeLayout mFlInspectorSeal;

    @Override
    public void release() {
        if (mItem0 != null) mItem0.release();
        if (mItem1 != null) mItem1.release();
        if (mItem2 != null) mItem2.release();
        if (mItem3 != null) mItem3.release();
        if (mItem4 != null) mItem4.release();
        if (mItem5 != null) mItem5.release();
        EventBus.getDefault().unregister(this);
        super.release();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCameraEvent(CameraEvent event) {
        cameraReload = true;
        initData();
    }

    public AcceptCheck(MainRepairActivity activity) {
        super(activity);
        EventBus.getDefault().register(this);
    }

    @Override
    protected View initView() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.page_accept_check, null);
        FrameLayout fl5 = (FrameLayout) inflate.findViewById(R.id.fl5);
        FrameLayout fl4 = (FrameLayout) inflate.findViewById(R.id.fl4);
        FrameLayout fl3 = (FrameLayout) inflate.findViewById(R.id.fl3);
        FrameLayout fl2 = (FrameLayout) inflate.findViewById(R.id.fl2);
        FrameLayout fl1 = (FrameLayout) inflate.findViewById(R.id.fl1);
        FrameLayout fl0 = (FrameLayout) inflate.findViewById(R.id.fl0);

        mScrollView = inflate.findViewById(R.id.scrollView);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mScrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if (scrollY > 500)
                        mScrollY = scrollY;
                    Log.v("AcceptCheck", "onScrollChange:                                       scrollY:" + scrollY);
                }
            });
        }

        mEtRemark = inflate.findViewById(R.id.etRemark);
        mFlUserSeal = inflate.findViewById(R.id.flUserSeal);
        mFlInspectorSeal = inflate.findViewById(R.id.flInspectorSeal);

        mItem0 = new AcceptItem0(mActivity);
        fl0.addView(mItem0.getItemView());
        mItem1 = new AcceptItem1(mActivity);
        fl1.addView(mItem1.getItemView());
        mItem2 = new AcceptItem2(mActivity);
        fl2.addView(mItem2.getItemView());
        mItem3 = new AcceptItem3(mActivity);
        fl3.addView(mItem3.getItemView());
        mItem4 = new AcceptItem4(mActivity);
        fl4.addView(mItem4.getItemView());
        mItem5 = new AcceptItem5(mActivity);
        fl5.addView(mItem5.getItemView());
        initData();
        return inflate;
    }

    @Override
    public void bindData(AcceptCheckInfo info) {
        SealHelper sealHelper = new SealHelper(mActivity) {
            @Override
            protected void onSeal() {
                final String userId0 = SPUtil.getString(SPKey.Account.USER_ID);
                final String remark = mEtRemark.getText().toString().trim();
                AcceptCheck.this.mActivity.sealByCard("", new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds == null || stampIds.size() == 0) {
                            ToastUtil.showCenterToast("无印章！");
                        } else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            StaticPresenter.stampRoutingSheetContent(userId0, AcceptCheck.this.mActivity.getBillId(), 1, "1", userId, stampIds.get(0).stampId, remark, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    //重新加载数据
                                    initData();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
            }
        };
        sealHelper.bindData(new SealHelper.SealBean(true, info.technicianStampId, info.technicianId, info.technicianStampTypeCode, info.technicianStampCode, info.technicianDate, info.technician, true));
        mFlUserSeal.removeAllViews();
        mFlUserSeal.addView(sealHelper.getItemView());

        SealHelper sealHelper1 = new SealHelper(mActivity) {
            @Override
            protected void onSeal() {
                final String userId0 = SPUtil.getString(SPKey.Account.USER_ID);
                final String remark = mEtRemark.getText().toString().trim();
                AcceptCheck.this.mActivity.sealByCard("", new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds == null || stampIds.size() == 0) {
                            ToastUtil.showCenterToast("无印章！");
                        } else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            StaticPresenter.stampRoutingSheetContent(userId0, AcceptCheck.this.mActivity.getBillId(), 1, "2", userId, stampIds.get(0).stampId, remark, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    AcceptCheck.this.mActivity.updateProgress();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
            }
        };
        sealHelper1.bindData(new SealHelper.SealBean(true, info.inspectorStampId, info.inspectorId, info.inspectorStampTypeCode, info.inspectorStampCode, info.inspectorDate, info.inspector, !TextUtils.isEmpty(info.technicianId)));
        mFlInspectorSeal.removeAllViews();
        mFlInspectorSeal.addView(sealHelper1.getItemView());

        mItem0.bindData(info.T1);
        mItem1.bindData(info.T2);
        mItem2.bindData(info.T3);
        mItem3.bindData(info.T4);
        mItem4.bindData(info.T5);
        mItem5.bindData(info.T6);
        mEtRemark.setText(info.remark);

    }

    private void initData() {
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.show();
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getReceiveRecordSheet(userId, mActivity.getBillId(), new CallBack<AcceptCheckInfo>() {
            @Override
            public void onCompleted(AcceptCheckInfo acceptCheckBean) {
                dialog.dismiss();
                bindData(acceptCheckBean);
                if (cameraReload) {
                    cameraReload = false;
                    if (mScrollView != null) {
                        App.getHandler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mScrollView.smoothScrollTo(0, mScrollY);
                            }
                        }, 200);
                    }
                }
            }

            @Override
            public void onError() {
                dialog.dismiss();
            }
        });
        StaticPresenter.getDictByIdOrCode(userId, null, "ReceivingCondition", 1, 30, new CallBack<List<DictBean>>() {
            @Override
            public void onCompleted(List<DictBean> dictBeans) {
                mAcceptStatus = dictBeans;
            }

            @Override
            public void onError() {

            }
        });
    }
}
